package com.server.service.rule;



import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import org.jdom.JDOMException;
import com.client.service.rule.DecisionMakerService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.server.rule.DecisionMakerManager;
import com.server.rule.component.RuleDecision;
import com.server.rule.component.integrated.RuleDecisionEmptyProposition;
import com.server.rule.component.integrated.RuleDecisionWithProposition;
import com.server.rule.data.Data;
import com.server.rule.data.ExceptionTypeData;
import com.server.rule.data.loader.AssureInformationLoader;
import com.server.rule.data.loader.InformationLoader;
import com.server.rule.data.loader.InformationLoaderFactory;
import com.server.store.PoolManager;
import com.server.store.data.AssureManager;
import com.server.store.data.PropositionManager;
import com.shared.data.IData;
import com.shared.data.TypeIData;
import com.shared.model.Apporteur;
import com.shared.model.Assure;
import com.shared.model.Proposition;
import com.shared.model.Tarification;
import com.shared.rule.TypeDataRule;
import com.shared.rule.TypeDecision;

/**
 * Implémentation du service de gestion des {@link DecisionMaker}
 * @author queyroi
 *
 */
@SuppressWarnings("serial")
public class DecisionMakerServiceImpl extends RemoteServiceServlet implements DecisionMakerService{

	
	
	public String operate(String nomDM,IData input,TypeIData type) {
		Data entryData = new Data();
		InformationLoader informationLoader = InformationLoaderFactory.get().getInformationLoader(type, input);		
		entryData.addInformationLoader(informationLoader);
		try {
			entryData.loadData();			
		} catch (IllegalArgumentException e1) {
			System.out.println(e1.getMessage());
			throw new IllegalArgumentException(e1.getClass().getName()+" "+e1.getMessage());
		} catch (IllegalAccessException e) {
			System.out.println(e.getMessage());
			throw new IllegalArgumentException(e.getClass().getName()+" "+e.getMessage());
		} catch (JDOMException e) {
			System.out.println(e.getMessage());
			throw new IllegalArgumentException(e.getClass().getName()+" "+e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
			throw new IllegalArgumentException(e.getClass().getName()+" "+e.getMessage());
		} 
		
		RuleDecisionWithProposition decisionDefault = new RuleDecisionEmptyProposition("Aucune");	
		List<RuleDecision> decisionFinales = new ArrayList<RuleDecision>();
		try {
			decisionFinales =  DecisionMakerManager.get().getDM(nomDM).operate(entryData, decisionDefault);			
		} catch (JDOMException e) {
			
			throw new IllegalArgumentException(e.getClass().getName()+" "+e.getMessage());
		} catch (ExceptionTypeData e) {
			
			throw new IllegalArgumentException(e.getClass().getName()+" "+e.getMessage());
		} catch (IOException e) {
			
			throw new IllegalArgumentException(e.getClass().getName()+" "+e.getMessage());
		} 
		Proposition proposition =null;
		for(RuleDecision decision : decisionFinales){
			if(((RuleDecisionWithProposition) decision).getTypeDecision()!=TypeDecision.EMPTY){	
				proposition = ((RuleDecisionWithProposition) decision).createProposition();			
				((PropositionManager) PoolManager.get().getDataManager(TypeIData.PROPOSITION)).save(proposition);
				((Assure) input).addProposition(proposition);
				((AssureManager) PoolManager.get().getDataManager(TypeIData.ASSURE)).save((Assure) input);
			}
		}		
		return "sucess";
	}

	public String[] getAvailableDM() {		
		String[] result = DecisionMakerManager.get().availableDM().toArray(new String[0]);		
		return result;
	}

	public String getDMasString(String nomDM) {
		try {
			return DecisionMakerManager.get().getDMasString(nomDM);
		} catch (JDOMException e) {
			throw new IllegalArgumentException(e.getClass().getName()+" "+e.getMessage());
		} catch (IOException e) {
			throw new IllegalArgumentException(e.getClass().getName()+" "+e.getMessage());
		}catch(NullPointerException e){
			throw new IllegalArgumentException("Erreur dans "+nomDM+". Chargement impossible");
		}
	}

	public String saveDM(String nomDM, String contents) {
		try {
			
			DecisionMakerManager.get().saveDM(nomDM, contents);
		} catch (NullPointerException e) {
			throw new IllegalArgumentException(e.getClass().getName()+" "+e.getMessage());
		} catch (JDOMException e) {
			throw new IllegalArgumentException(e.getClass().getName()+" "+e.getMessage());
		} catch (IOException e) {
			throw new IllegalArgumentException(e.getClass().getName()+" "+e.getMessage());
		}
		return "success";
	}

	public Map<String, TypeDataRule> getData(String type) throws IllegalArgumentException{
		TypeIData typeData = TypeIData.getEnum(type);		
		try {
			return InformationLoaderFactory.get().getInformationLoader(typeData).getAvailableData();
		} catch (IllegalArgumentException e) {
			throw e;
		} catch (IllegalAccessException e) {
			throw new IllegalArgumentException("Echec lors de la requete");
		}	
		
	}

}
